package NIO;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

public class Channel {

    public static void main(String[] args) throws Exception{

        // 创建RandomAccessFile对象，指定源文件
        RandomAccessFile infile = new RandomAccessFile("src/NIO/科比.jpg","rw");
        // 获取源文件FileChannel通道
        FileChannel inchannel = infile.getChannel();

        // 创建RandomAccessFile对象，指目标文件
        RandomAccessFile outfile = new RandomAccessFile("src/NIO/forever.jpg","rw");
        // 获取复制目标文件FileChannel通道
        FileChannel outchannel = outfile.getChannel();

        // 使用transferTo方法整体复制
        long transferTo = inchannel.transferTo(0,inchannel.size(),outchannel);

        if (transferTo > 0) {
            System.out.println("复制成功！！！");
        }

        // 关闭资源
        infile.close();
        inchannel.close();
        outfile.close();
        outchannel.close();
    }
}
